package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductNaturallyDao;
import yl.hs.bmipfull.pojo.production.ProductNaturallyEntity;
import yl.hs.bmipfull.service.production.IProductNaturallyService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 作用:  天然气充值记录服务层接口实现<br/>
 * 创建时间: 2023年09月11日 10时41分36秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProductNaturallyServiceImpl extends ProductionBaseService implements IProductNaturallyService {

    @Autowired
    IProductNaturallyDao dao;

    @Override
    public List<ProductNaturallyEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductNaturallyEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductNaturallyEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductNaturallyEntity.class);
        var datas = queryList(pageQuery);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        PageResult<ProductNaturallyEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        result.setObject(datas.stream().map(ProductNaturallyEntity::getQuantity).reduce(BigDecimal::add));
        return result;
    }


    @Override
    public ProductNaturallyEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveproduct_naturally(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductNaturallyEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductNaturallyEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductNaturallyEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入天然气充值记录信息", Tl.toJson(entity), ProductNaturallyEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新天然气充值记录信息", Tl.toJson(entity), ProductNaturallyEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        var sns = sn.split(",");
        Integer result = dao.changeStateBySN(sns, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sns, ProductNaturallyEntity.class);
        }
        return result;
    }
}